#include <game/controls/ControlFactory.h>
#include <game/controls/Button.h>

#include <system/assert.h>

ARELLO_GAME_NAMESPACE_DECL_START
namespace controls {

ControlFactory::ControlFactory()
{
	Register("Button", &Button::Creator);
}

void ControlFactory::Register(const std::string& name, ControlCreator creator)
{
	std::pair<std::map<std::string, ControlCreator>::iterator, bool> insertResult = 
		this->creators.insert(std::make_pair(name, creator));

	LIBA_ASSERT(insertResult.second);
}

boost::shared_ptr<Control> ControlFactory::Create(const std::string& name, const liba::Atom& controlName, liba::Object2D* obj2d) const
{
	std::map<std::string, ControlCreator>::const_iterator i =
		this->creators.find(name);

	if (i != this->creators.end())
	{
		return i->second(controlName, obj2d);
	}
	return boost::shared_ptr<Control>();
}

bool ControlFactory::IsValidControlName(const std::string& name) const
{
	return this->creators.find(name) != this->creators.end();
}

} // namespace controls
ARELLO_GAME_NAMESPACE_DECL_END